<?php

namespace App\Trip\Traits;

use Symfony\Component\HttpFoundation\Request;

trait IpAddress
{
    // 获取 ip地址
    public function getIpAddress(): string
    {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }

        if (getenv('HTTP_X_REAL_IP')) {
            $ip = getenv('HTTP_X_REAL_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip  = getenv('HTTP_X_FORWARDED_FOR');
            $ips = explode(',', $ip);
            $ip  = $ips[0];
        } elseif (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        } else {
            $ip = Request::createFromGlobals()->getClientIp();
        }

        return (string)$ip;
    }
}